/*
 * Copyright (C) 2015 Brent Marriott
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hookedonplay.decoviewlib.events;

import com.hookedonplay.decoviewlib.charts.DecoDrawEffect;

import ohos.agp.components.Component;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import org.jetbrains.annotations.NonNls;

/**
 * Event manager for processing {@link DecoEvent} at the scheduled time (or immediately if no
 * delay is set). This class is also responsible for processing the hide/show fade effects of linked
 * views.
 *
 * Each {@link com.hookedonplay.decoviewlib.DecoView} contains one DecoEventManager, which can
 * handle any number of {@link DecoEvent}
 */
public class DecoEventManager {
    /**
     * Handler to manage firing events at given delays
     */
    private final EventHandler mHandler = new EventHandler(EventRunner.getMainEventRunner());//EventRunner.getMainEventRunner());

    private final ArcEventManagerListener mListener;

    public DecoEventManager(@NonNls ArcEventManagerListener listener) {
        mListener = listener;
    }

    /**
     * Add a {@link DecoEvent} to the schedule to be processed at the required time
     *
     * @param event DecoEvent to add
     */
    public void add(@NonNls final DecoEvent event) {
        /**
         * Determine if we need to show and linked views attached to the event. This is useful
         * when scheduling an event at a later time and have the linked view automatically
         * faded in when the event is started. The user could do this themselves by setting
         * a listener on the event start.
         */
        final boolean show =
                (event.getEventType() == DecoEvent.EventType.EVENT_SHOW)
                        || (event.getEffectType() == DecoDrawEffect.EffectType.EFFECT_SPIRAL_OUT)
                        || (event.getEffectType() == DecoDrawEffect.EffectType.EFFECT_SPIRAL_OUT_FILL);

        mHandler.postTask(
                (new Runnable() {
                    @Override
                    public void run() {
                        if (show && event.getLinkedViews() != null) {
                            for (Component view : event.getLinkedViews()) {
                                view.setVisibility(Component.VISIBLE);
                            }
                        }
                        if (mListener != null) {
                            mListener.onExecuteEventStart(event);
                        }
                    }
                }),
                event.getDelay());
    }

    /**
     * Remove any existing delayed messages from the handler
     */
    public void resetEvents() {
        mHandler.removeTask(null);
        mHandler.removeAllEvent();
    }

    /**
     * Callback interface for notification of event to be processed
     */
    public interface ArcEventManagerListener {
        void onExecuteEventStart(@NonNls DecoEvent event);
    }
}